#include "ViewHeader.h"
#include "ViewsContainer.h"

///<************************************************************************************************************>
///<************************************* region CONSTANTS DEFINITION ******************************************>
///<************************************************************************************************************>

const QString CIGui::ViewHeader::STYLE_SHEET = QString (
                                                    "QObject {"
                                                        "font-family: Helvetica;"
                                                        //"font: bold;"
                                                        //"font-size: 8pt;"
                                                        //"color: black;"
                                                    "}"
                                                     "QFrame {"
                                                         "margin-left: 3px;"
                                                         "border: 1px solid gray;"
                                                         "border-radius: 3px;"
                                                         "padding: 0px 18px 1px 20px;"
                                                         + GENERAL_BACKGROUND +
                                                     "}"

                                                    "QComboBox {"
                                                         //"image: url("+IMAGES_FOLDER+"camera-selector.png);"
                                                         "margin-left: 3px;"
                                                         "border: 1px solid gray;"
                                                         "border-radius: 3px;"
                                                         "padding: 1px 18px 1px 20px;"
                                                     "}"


                                                     "QComboBox:editable {"
                                                        "background: rgba(0,0,0,0);"
                                                     "}"

                                                     "QComboBox:!editable:hover, QComboBox::drop-down:hover {"
                                                         "background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,"
                                                                                    "stop:0 rgba(204, 225, 255, 255),"
                                                                                    "stop:1 rgba(241, 241, 248, 255));"
                                                     "}"

                                                     "QComboBox:!editable, QComboBox::drop-down:editable {"
                                                          "background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, "
                                                                                        "stop:0 rgba(233, 237, 247, 255), "
                                                                                        "stop:1 rgba(145, 161, 174, 255));"
                                                     "}"

                                                      /* QComboBox gets the "on" state when the popup is open */
                                                     "QComboBox:!editable:on, QComboBox::drop-down:editable:on {"
                                                          "background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 "
                                                                                        "rgba(145, 161, 174, 255), stop:1 rgba(233, 237, 247, 255));"
                                                     "}"

                                                     //"QComboBox:on {" /* shift the text when the popup opens */
                                                     //    "padding: 1px 18px 1px 2px;"
                                                     //"}"

                                                     "QComboBox::drop-down {"
                                                         "subcontrol-origin: padding;"
                                                         "subcontrol-position: top right;"
                                                         "width: 15px;"

                                                         "border-left-width: 1px;"
                                                         "border-left-color: darkgray;"
                                                         "border-left-style: solid;" /* just a single line */
                                                         "border-top-right-radius: 3px;" /* same radius as the QComboBox */
                                                         "border-bottom-right-radius: 3px;"
                                                     "}"

                                                     "QComboBox::down-arrow {"
                                                        "image: url("+IMAGES_FOLDER+"downarrow.png);"
                                                     "}"

                                                     "QComboBox::down-arrow:on {" /* shift the arrow when popup is open */
                                                         "top: 1px;"
                                                         "left: 1px;"
                                                     "}"

                                                     "QToolButton {"
                                                        "margin: 0px 5px 0px 10px;"
                                                     "}"

                                                    "QToolButton::hover {"
                                                        "border: 1px solid gray;"
                                                        "border-radius: 5px;"
                                                        "background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,"
                                                                                    "stop:0 rgba(204, 225, 255, 255),"
                                                                                    "stop:1 rgba(241, 241, 248, 255));"
                                                    "}"

                                                    "QToolButton:pressed, QToolButton:checked, QToolButton:selected {"
                                                        "border: 1px solid black;"
                                                        "border-radius: 5px;"
                                                        "background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,"
                                                                                    "stop: 0 #ccf, stop: 1 #eef);"
                                                        "padding: 1px 0px 0px 1px;"
                                                    "}"
                                                     );
/***************************************************************************************************************
************************************** endregion CONSTANTS DEFINITION ******************************************
****************************************************************************************************************/






/// <summary>
/// Constructor
/// </summary>
/// <param name="parent"> The widget parent </param>
/// <param name="v"> The view associated </param>
CIGui::ViewHeader::ViewHeader (QWidget* parent, CIView::View* v) : QToolBar (parent), view(v) {
    setSizePolicy (QSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding));
    setStyleSheet (STYLE_SHEET);

    setMinimumSize (0, 22);
    setMaximumSize (10000, 22);

    CreateActions();
    CreateWidgets();
}

/// <summary>
/// Destructor
/// </summary>
CIGui::ViewHeader::~ViewHeader() {
    //Deleting combo boxes
    delete cameraSelector;              //A combo box to select the needed visual
    //delete polygonModeSelector;         //A combo box to select the polygon mode Wireframe, Fill,...

    //Deleting Icons Actions
    delete resetCameraAct;              //Action to reset the camera parameters
    delete lightOnOffAct;               //Action to show the camera's options
    //delete infoFilterAct;               //Action to select the information to be showed
    delete fullScreenAct;               //Action to select the full screen mode
}








///<************************************************************************************************************>
///<****************************************** region Public SLOTS *********************************************>
///<************************************************************************************************************>

/// <summary>
/// Slot method called to show the view in fullscreen mode
/// </summary>
void CIGui::ViewHeader::ToggleFullScreenMode() {
    ViewsContainer* parent = (ViewsContainer*)parentWidget();

    parent->ToggleViewFullScreenMode (view);            //Toggles the full screen mode for the view
}

/// <summary>
/// Slot method called to reset the camera parameters
/// </summary>
void CIGui::ViewHeader::ResetCamera() {
    view->ResetCurrentCamera();
}

/// <summary>
/// Slot method called whenever lighting is enableb/disableb
/// </summary>
void CIGui::ViewHeader::LightOnOff (bool l) {
    if (l)
        lightOnOffAct->setIcon (QIcon (IMAGES_FOLDER + "light-on.png"));
    else
        lightOnOffAct->setIcon (QIcon (IMAGES_FOLDER + "light-off.png"));
}
/***************************************************************************************************************
******************************************* endregion Public SLOTS *********************************************
****************************************************************************************************************/










///<************************************************************************************************************>
///<**************************************** region Utility Methods ********************************************>
///<************************************************************************************************************>

/// <summary>
/// Method called to create the actions
/// </summary>
void CIGui::ViewHeader::CreateActions() {
    QWidget* mainWindow;
    QWidget* tmp = this;

    while (tmp) {
        mainWindow = tmp;
        tmp = tmp->parentWidget();
    }

    //Icons Actions
    resetCameraAct = new QAction (QIcon (IMAGES_FOLDER + "camera-black.png"), tr ("&Reset Camera..."), mainWindow);
    //resetCameraAct->setShortcut (tr ("C"));
    resetCameraAct->setStatusTip (tr ("Reset the camera parameters"));
    connect (resetCameraAct, SIGNAL (triggered()), this, SLOT (ResetCamera()));
    addAction (resetCameraAct);

    //Icons Actions
    lightOnOffAct = new QAction (QIcon (IMAGES_FOLDER + "light-on.png"), tr ("&Enable/Disable lighting..."), mainWindow);
    //lightOnOffAct->setShortcut (tr ("L"));
    lightOnOffAct->setStatusTip (tr ("Enable/Disable lighting"));
    connect (lightOnOffAct, SIGNAL (triggered()), view, SLOT (ToggleLighting ()));
    connect (view, SIGNAL (LightingEnabled (bool)), this, SLOT (LightOnOff (bool)));
    addAction (lightOnOffAct);

    //addSeparator();

    //Adding a QSpacerItem to move the next icons to the right
    QWidget* w = new QWidget (this);
    QHBoxLayout* layout = new QHBoxLayout (w);
    layout->addItem (new QSpacerItem (1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
    this->addWidget (w);

    fullScreenAct = new QAction (QIcon (IMAGES_FOLDER + "fullscreen.png"), tr ("&Full Screen..."), mainWindow);
    //fullScreenAct->setShortcut (tr ("F"));
    fullScreenAct->setStatusTip (tr ("Toggle fullscreen mode"));
    connect (fullScreenAct, SIGNAL (triggered()), this, SLOT (ToggleFullScreenMode()));
    addAction (fullScreenAct);
}

/// <summary>
/// Method called to create the combo box widgets
/// </summary>
void CIGui::ViewHeader::CreateWidgets() {
    CreateCameraSelector();
    CreatePolygonModeSelector();
}

/// <summary>
/// Method called to create the camera-selector combo box
/// </summary>
void CIGui::ViewHeader::CreateCameraSelector() {
    cameraSelector = new QComboBox (this);
    cameraSelector->setMaximumHeight (18);
    cameraSelector->setMinimumWidth (100);
    cameraSelector->setStatusTip ("Select a camera");

    //Adding items
    cameraSelector->addItem (TOP_VIEW);
    cameraSelector->addItem (FRONT_VIEW);
    cameraSelector->addItem (RIGHT_VIEW);
    cameraSelector->addItem (USER_VIEW);

    connect (cameraSelector, SIGNAL (currentIndexChanged (int)), view, SLOT (SelectCamera (int)));

    //Setting the current-combo-box-element selected according to the camera's type of the view
    cameraSelector->setCurrentIndex (view->camera->type);

    //Adding combo box widgets
    insertWidget (resetCameraAct, cameraSelector);
}

/// <summary>
/// Method called to create the polygon-mode-selector combo box
/// </summary>
void CIGui::ViewHeader::CreatePolygonModeSelector() {
    polygonModeSelector = new QComboBox (this);
    polygonModeSelector->setMaximumHeight (18);
    polygonModeSelector->setStatusTip ("Select the shading model");
    polygonModeSelector->setSizeAdjustPolicy (QComboBox::AdjustToContents);
    polygonModeSelector->setStyleSheet (
                                                    "QObject {"
                                                        "font-family: Helvetica;"
                                                        "text-align: center;"
                                                        //"font: bold;"
                                                        //"font-size: 8pt;"
                                                        //"color: black;"
                                                    "}"
                                                     "QFrame {"
                                                         "margin-left: 0px;"
                                                         "border: 1px solid gray;"
                                                         "border-radius: 3px;"
                                                         "padding: 1px 0px 1px 0px;"
                                                         + GENERAL_BACKGROUND +
                                                     "}"

                                                    "QComboBox {"
                                                         "image: url("+IMAGES_FOLDER+"downarrow_smaller.png);"
                                                         "margin-left: 0px;"
                                                         "border: 0px;"
                                                         "border-radius: 0px;"
                                                         "padding: 0px 0px 0px 0px;"
                                                         "text-align: center;"
                                                         "min-width: 2em;"
                                                     "}"

                                                     "QComboBox:editable {"
                                                        "background: rgba(0,0,0,0);"
                                                     "}"

                                                     "QComboBox:!editable:hover, QComboBox::drop-down:hover {"
                                                         "background: rgba(0,0,0,0);"
                                                    "}"

                                                     "QComboBox:!editable, QComboBox::drop-down:editable {"
                                                          "background: rgba(0,0,0,0);"
                                                    "}"

                                                      /* QComboBox gets the "on" state when the popup is open */
                                                     "QComboBox:!editable:on, QComboBox::drop-down:editable:on {"
                                                          "background: rgba(0,0,0,0);"
                                                    "}"

                                                     "QComboBox::drop-down {"
                                                         "subcontrol-origin: padding;"
                                                         "subcontrol-position: top right;"
                                                         "width: 22px;"

                                                         "border-left-width: 0px;"
                                                         "border-left-color: darkgray;"
                                                         "border-left-style: solid;" /* just a single line */
                                                         "border-top-right-radius: 0px;" /* same radius as the QComboBox */
                                                         "border-bottom-right-radius: 0px;"
                                                     "}"

                                                     "QComboBox::down-arrow {"
                                                        "image: url();"
                                                     "}"

                                                     "QComboBox::down-arrow:on {" /* shift the arrow when popup is open */
                                                         "top: 0px;"
                                                         "left: 0px;"
                                                     "}"

                                                     "QComboBox::text { alignment: center; }"
                                         );

    //Adding items
    polygonModeSelector->addItem (WIREFRAME_MODE);
    polygonModeSelector->addItem (FLAT_MODE);
    polygonModeSelector->addItem (SMOOTH_MODE);

    polygonModeSelector->setCurrentIndex (0);

    connect (polygonModeSelector, SIGNAL (currentIndexChanged (int)), view, SLOT (SelectShadeModel (int)));

    //Adding combo box widgets
    insertWidget (fullScreenAct, polygonModeSelector);
}
/***************************************************************************************************************
***************************************** endregion Utility Methods ********************************************
****************************************************************************************************************/
